package Java并发编程艺术第一章;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-09-10
 * Time: 16:39
 */
public class ConcurrencyTest {

    private static final long count = 20_0000_0000;

    //创建两个线程并发执行
    private static void concurrency() {
        long start =System.currentTimeMillis();
        Thread t1 = new Thread(()->{
           int a =0;
           for(int i =0;i<=count;++i){
               a++;
           }
        });
        Thread t2 = new Thread(()->{
            int b =0;
            for(int i =0;i<=count;++i){
                b++;
            }
        });
          t1.start();
          t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end =System.currentTimeMillis();
        long time = end - start;
        System.out.println("concurrency  :" + time + "ms");
    }

    private static void serial() {
        long start =System.currentTimeMillis();
        int a = 0;
        for(int i =0;i<=count;++i){
            a++;
        }
        a = 0;
        for(int i =0;i<=count;++i){
            a++;
        }
        long end =System.currentTimeMillis();
        long time = end - start;
        System.out.println("serial  :"+ time + "ms");
    }


    public static void main(String[] args){
        concurrency();//并行
        serial();//串行
    }

//    private static final long count = 100000_000;
//
//    private static void concurrency(){
//        long start = System.currentTimeMillis();
//        Thread t = new Thread(()->{
//           int a =0;
//           for(long i =0;i<count;++i){
//               a+=5;
//           }
//        });
//        t.start();
//        int b =0;
//        for(long i =0;i<count;++i){
//            b--;
//        }
//        try {
//            t.join();
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        long time = System.currentTimeMillis() - start;
//        System.out.println("并发消耗时间 :"+time+"ms");
//    }
//
//    private static void serial(){
//        long start = System.currentTimeMillis();
//            int a =0;
//            for(long i =0;i<count;++i){
//                a+=5;
//            }
//        int b =0;
//        for(long i =0;i<count;++i){
//            b--;
//        }
//        long time = System.currentTimeMillis() - start;
//        System.out.println("串行消耗时间 :"+time+"ms");
//    }
//    public static void main(String[] args) {
//        concurrency();//并发
//        serial();//串行
//    }
//
//



//    private static final long count = 100000l;
//    private static void concurrency() throws InterruptedException {
//        long start = System.currentTimeMillis();
//        Thread thread = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                int a = 0;
//                for (long i = 0; i < count; i++) {
//                    a += 5;
//                }
//            }
//        });
//        thread.start();
//        int b = 0;
//        for (long i = 0; i < count; i++) {
//            b--;
//        }
//        long time = System.currentTimeMillis() - start;
//        thread.join();
//        System.out.println("concurrency :" + time+"ms");
//    }
//
//    private static void serial() {
//        long start = System.currentTimeMillis();
//        int a = 0;
//        for (long i = 0; i < count; i++) {
//            a += 5;
//        }
//        int b = 0;
//        for (long i = 0; i < count; i++) {
//            b--;
//        }
//        long time = System.currentTimeMillis() - start;
//        System.out.println("serial:" + time+"ms");
//    }
//
//    public static void main(String[] args) throws InterruptedException {
//        concurrency();//并行
//        serial();//串行
//    }

}
